layui.define(['layer', 'form', "upload"], function (exports) {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form();
    //上传附件
    layui.upload({
        url: '/html/file/srv/upd_file_send_attachment.php/' //上传接口
        , success: function (res) { //上传成功后的回调
            if ($("#attachment").val()) {
                var attachment = eval($("#attachment").val());
            } else {
                var attachment = [];
            }
            $.each(res, function (i, d) {
                if (d.ret == 'ok') {
                    var html = '<a class="attachment_one" href="' + d.save_path + '">' + d.attachment_name + '</a><a class="attachment_one_del" href="javascript:;">[删除]</a>';
                    $("#attachment_box").append($(html));
                    var attach = {'attachment_id': d.attachment_id, 'attachment_name': d.attachment_name, 'save_path': d.save_path};
                    attachment.push(attach);
                }
            })
            //console.log(JSON.stringify(attachment));
            if (attachment)
                $("#attachment").val(JSON.stringify(attachment));

        }
    });
    //全选
    form.on('checkbox(check)', function (data) {
        if (data.elem.checked) {
            $("input[name='checkbox']").prop("checked", true);
        } else {
            $("input[name='checkbox']").prop("checked", false);
        }
        form.render('checkbox');
    });
    //'新增文件交换
    $("#btn_add").click(function () {
        $(".file_turn_to").remove();
        $("#reset").show();
        layer.open({
            type: 1,
            title: '新增文件交换',
            shadeClose: false,
            shade: 0.8,
            area: ['100%', '100%'],
            full: true,
            content: $(".send_open"),
        });
        $("#reset").click();
    });
    //添加收信人
    $("#select_accept").click(function () {
        var userid = $("#accept_id").val();
        layer.open({
            type: 2,
            title: '添加收信人',
            shadeClose: false,
            shade: 0.8,
            area: ['100%', '100%'],
            full: true,
            content: './?t=file_addressee&userid=' + userid, //iframe的url
        });
    })
    //提交
    $("#btn_save").click(function () {
        if ($.trim($("#title").val()) == '') {
            layer.msg("请填写文件名");
            return false;
        }
        if ($.trim($("#description").val()) == '') {
            layer.msg("请填写文件简介");
            return false;
        }

        if ($.trim($("#accept_id").val()) == '') {
            layer.msg("请填写收件人");
            return false;
        }
        $(this).parents("form").submit();
    })
    //重置
    $("#reset").click(function () {
        var attachment = $.trim($("#attachment").val());
        if (typeof attachment === 'string') {
            attachment = eval(attachment);
        }

        if (attachment) {
            attachment.each(function (v) {
                var file_path = v.save_path;
                delField(file_path);
            })
        }
        $(this).parents("form").find("input").val("");
        $(".div_content,#attachment_box").html("");
    })
    //清空收件人
    $("#del_accept").click(function () {
        $("#accept_id").val("");
        $(".div_content").html("");
    })
    //删除上传文件
    $(document).on("click", ".attachment_one_del", function () {
        var file_path = $(this).prev().attr("href");
        /*
         ajax删除文件 
         */
        delField(file_path);

        var index = $(this).index(".attachment_one_del");
        var attachment = $("#attachment").val();
        if (typeof attachment === 'string') {
            attachment = eval(attachment);
        }
        if (attachment.length > 0) {
            attachment = attachment.splice(index, 1);  //删除一个对象
            $("#attachment").val(JSON.stringify(attachment));
        }
        $(this).prev().remove();
        $(this).remove();
    })
    //删除文件ajax
    function delField(file_path) {
        $.post("/srv/sdo.php", {file_path: file_path, tpl: "del_file"}, function (res) {
            if (res.status)
                console.log(res.tips);
        });
    }

    //导出
    $("#btn_out").click(function () {
        var id = "";
        if ($("[name='checkbox']:checked").length > 0) {
            $("[name='checkbox']:checked").each(function () {
                id += $(this).val() + ",";
            })
            location.href = "/html/file/srv/file_export.php?id=" + id;

        } else {
            layer.msg("请选择需要导出的选项")
        }
    })
    //转发
    $("#btn_turn_to").click(function () {
        var id = "";
        if ($("[name='checkbox']:checked").length > 0) {
            $("[name='checkbox']:checked").each(function () {
                id += "'" + $(this).val() + "',";
            })
            $.ajax({
                type: "post",
                url: "/html/file/srv/file_turn_to.php",
                data: {id: id},
                dataType: "json",
                success: function (res) {
                    $(".file_turn_to").remove();
                    $("#reset").hide();
                    layer.open({
                        type: 1,
                        title: '文件转发',
                        shadeClose: false,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        full: true,
                        content: $(".send_open"),
                    });
                    $("#reset").click();

                    if (res.status) {
                        var html = '<div class="layui-form-item file_turn_to">\n\
                            <label class="layui-form-label">附件文档</label>\n\
                            <div class="layui-input-block">';
                        var attachment_old = [];
                        $.each(res.name, function (i, v) {
                            html += '<div style="display:inline-block;margin-left:20px"><a href="' + (res.dir + res['id'][i] + "/" + v) + '">' + v + '</a>\n\
                                    <a class="read">阅读</a> <a class="edit">编辑</a>\n\
                              \n\
                            </div>';
                            var attach = {'attachment_id': res['id'][i], 'attachment_name': v};
                            attachment_old.push(attach);
                        })
                        html += '<input type="text" name="attachment_old" value=\'' + JSON.stringify(attachment_old) + '\' style="display:none"></div></div>';
                        $("#form1 div.layui-form-item:eq(2)").after(html);
                    }

                }
            });

        } else {
            layer.msg("请选择需要转发的选项")
        }
    })
    //文件详情
    $(".file_info").click(function () {
        layer.open({
            type: 2,
            title: '详细信息',
            shadeClose: false,
            shade: 0.8,
            area: ['100%', '100%'],
            full: true,
            content: "/?t=file_info&id="+$(this).attr("data-id"),
        });
    })
    //列表多选删除
    $("#btn_del").click(function () {
        var id = "";
        if ($("[name='checkbox']:checked").length > 0) {
            $("[name='checkbox']:checked").each(function () {
                id += "'" + $(this).val() + "',";
            })
            $.ajax({
                type: "post",
                url: "/srv/sdo.php",
                data: {id: id, tpl: "del_file_upload"},
                dataType: "json",
                success: function (res) {
                    if (res.status) {
                        layer.msg(res.tips, {}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.tips);
                    }
                }
            });

        } else {
            layer.msg("请选择需要删除的选项")
        }
    })
    exports('file_send', {});
});